SyntaxError: "use strict" nicht erlaubt in einer Funktion mit nicht einfachen Parametern
Der JavaScript-Ausnahmefehler ""use strict"
nicht erlaubt in der Funktion" tritt auf,
wenn eine "use strict"
-Direktive am Anfang einer Funktion verwendet wird, die
Standardparameter,
Restparameter oder
Destructuring-Parameter enthält.
Meldung
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list (V8-based) SyntaxError: "use strict" not allowed in function with default parameter (Firefox) SyntaxError: "use strict" not allowed in function with rest parameter (Firefox) SyntaxError: "use strict" not allowed in function with destructuring parameter (Firefox) SyntaxError: 'use strict' directive not allowed inside a function with a non-simple parameter list. (Safari)
Fehlertyp
Was ist schiefgelaufen?
Eine "use strict"
-Direktive wird am Anfang einer Funktion verwendet, die einen der folgenden Parameter hat:
Eine "use strict"
-Direktive ist am Anfang solcher Funktionen gemäß der ECMAScript-Spezifikation nicht erlaubt.
Beispiele
Funktionsdeklaration
In diesem Fall hat die Funktion sum
die Standardparameter a=1
und b=2
:
function sum(a = 1, b = 2) {
// SyntaxError: "use strict" not allowed in function with default parameter
"use strict";
return a + b;
}
Sollte die Funktion im strict mode sein und es ist auch in Ordnung, dass das gesamte Skript oder die umgebende Funktion im strict mode ist, können Sie die "use strict"
-Direktive außerhalb der Funktion verschieben:
"use strict";
function sum(a = 1, b = 2) {
return a + b;
}
Funktionsausdruck
Ein Funktionsausdruck kann eine weitere Lösung verwenden:
const sum = function sum([a, b]) {
// SyntaxError: "use strict" not allowed in function with destructuring parameter
"use strict";
return a + b;
};
Dies kann in den folgenden Ausdruck umgewandelt werden:
const sum = (function () {
"use strict";
return function sum([a, b]) {
return a + b;
};
})();
Pfeilfunktion
Wenn eine Pfeilfunktion auf die Variable this
zugreifen muss, können Sie die Pfeilfunktion als umgebende Funktion verwenden:
const callback = (...args) => {
// SyntaxError: "use strict" not allowed in function with rest parameter
"use strict";
return this.run(args);
};
Dies kann in den folgenden Ausdruck umgewandelt werden:
const callback = (() => {
"use strict";
return (...args) => this.run(args);
})();